﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// ShortcutAction.xaml 的交互逻辑
    /// </summary>
    public partial class ShortcutAction : Window
    {
        private Designtime.Metadata.Scene _scene;
        private ObservableCollection<Designtime.Metadata.CustomAction> _CustomActions;
        public ShortcutAction(Designtime.Metadata.Scene scene)
        {
            InitializeComponent();
            _scene = scene;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _CustomActions = new ObservableCollection<Designtime.Metadata.CustomAction>();
            foreach (var action in _scene.GetShorcutAction())
            {
                _CustomActions.Add(action);
            }
            _ctlListView.ItemsSource = _CustomActions;
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            Designtime.Metadata.CustomAction newCustomAction = new Designtime.Metadata.CustomAction();
            Step window = new Step(newCustomAction);
            if (window.ShowDialog().Value)
            {
                _CustomActions.Add(newCustomAction);
            }
        }

        private void _ctlRemove_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlListView.SelectedItem != null)
            {
                _CustomActions.Remove(_ctlListView.SelectedItem as Designtime.Metadata.CustomAction);
            }
        }

        private void _ctlEdit_Click(object sender, RoutedEventArgs e)
        {
            EditSteps();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < _CustomActions.Count; i++)
            {
                Designtime.Metadata.CustomAction customAction = _CustomActions[i];
                if (customAction.Steps == null || customAction.Steps.Count == 0)
                {
                    CustomMessageBox.ShowWarning("请配置步骤");
                    return;
                }
            }
            if (UIContext.Current.CurrentSelectedNode != null && UIContext.Current.CurrentSelectedNode.NodeStatus == NodeStatus.CheckOut)
            {
                SaveShortcutActions();
                DialogResult = true;
            }
            else
            {
                List<SolutionTreeViewNode> nodeList = new List<SolutionTreeViewNode>();
                nodeList.Add(UIContext.Current.CurrentSelectedNode);
                CheckOutThread.Start(nodeList, ReportProgress, Completed);
            }
        }

        private void SaveShortcutActions()
        {
            _scene.SaveCustomAction(_CustomActions);
            Designtime.Metadata.LocalStorage.SaveSceneFile(UIContext.Current.LocalWorkSpace.LocalPath, UIContext.Current.CurrentSolution.ID, _scene);
        }

        private void Completed(bool hasError, int arg2, int arg3)
        {
            if (hasError)
            {
                Dispatcher.Invoke(() =>
                {
                    CustomMessageBox.ShowWarning("签出场景失败");
                });
            }
            else
            {
                SaveShortcutActions();
                Dispatcher.Invoke(() =>
                {
                    DialogResult = true;
                });
            }
        }

        private void ReportProgress(Guid arg1, string arg2, int arg3, int arg4)
        {
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void _ctlListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EditSteps();
        }

        private void EditSteps()
        {
            if(_ctlListView.SelectedItem != null)
            {
                Step window = new Step(_ctlListView.SelectedItem as Designtime.Metadata.CustomAction);
                window.ShowDialog();
            }
        }
    }
}
